package com.xci.sys.api;

import com.xci.core.base.RestMessage;
import com.xci.core.base.PageList;
import com.xci.core.domain.PageModel;
import com.xci.core.internal.Const;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.annotation.Authorize;
import com.xci.core.base.ApiController;
import com.xci.sys.entity.SysSeq;
import com.xci.sys.service.SeqService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 序列接口
 * @author 吕艳阳
 */
@ApiSort(7)
@Api(tags = "序列接口")
@Authorize
@RestController
@RequestMapping(value = "/api/sys/seq", produces = Const.P_JSON)
public class SeqApiController extends ApiController {
    @Resource
    private SeqService seqService;//序列服务

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新建序列")
    @Authorize(code = "sys.seq.insert")
    @PostMapping("/insert")
    public RestMessage insert(@RequestBody SysSeq entity) {
        return seqService.insert(entity);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改序列")
    @Authorize(code = "sys.seq.update")
    @PostMapping("/update")
    public RestMessage update(@RequestBody SysSeq entity) {
        return seqService.update(entity);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "删除序列")
    @ApiImplicitParam(name = "ids", value = "序列主键字符串", required = true)
    @Authorize(code = "sys.seq.delete")
    @PostMapping("/delete")
    public RestMessage delete(String ids) {
        return seqService.delete(ids);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "检查序列编码是否存在")
    @ApiImplicitParam(name = "code", value = "序列编码", required = true)
    @PostMapping("/existByCode/{code}")
    public RestMessage existByCode(@PathVariable String code) {
        return RestMessage.result(seqService.existByCode(code));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "查询单个序列")
    @ApiImplicitParam(name = "id", value = "序列主键", required = true)
    @PostMapping("/selectById/{id}")
    public RestMessage<SysSeq> selectById(@PathVariable String id) {
        return RestMessage.success(seqService.selectById(id));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "根据编码查询单个序列")
    @ApiImplicitParam(name = "code", value = "序列编码", required = true)
    @PostMapping("/selectByCode/{code}")
    public RestMessage<SysSeq> selectByCode(@PathVariable String code) {
        return RestMessage.success(seqService.selectByCode(code));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "根据编码查询当前序列值")
    @ApiImplicitParam(name = "code", value = "序列编码", required = true)
    @PostMapping("/selectCurrentValue/{code}")
    public RestMessage<Long> selectCurrentValue(@PathVariable String code) {
        return RestMessage.success(seqService.selectCurrentValue(code));
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "根据编码查询下一个序列值")
    @ApiImplicitParam(name = "code", value = "序列编码", required = true)
    @PostMapping("/selectNextValue/{code}")
    public RestMessage<Long> selectNextValue(@PathVariable String code) {
        return RestMessage.success(seqService.selectNextValue(code));
    }

    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "查询序列分页列表")
    @ApiImplicitParam(name = "key", value = "编码/名称关键字")
    @PostMapping("/selectPageList")
    public RestMessage<PageList<SysSeq>> selectPageList(PageModel pageModel, String key) {
        return RestMessage.success(PageList.of(seqService.selectPageList(key)));
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "查询序列列表")
    @ApiImplicitParam(name = "name", value = "编码/名称关键字")
    @PostMapping("/selectList")
    public RestMessage<List<SysSeq>> selectList(String name) {
        return RestMessage.success(seqService.selectList(name));
    }

    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "导出序列列表")
    @ApiImplicitParam(name = "name", value = "编码/名称关键字")
    @Authorize(code = "sys.seq.export")
    @PostMapping(value = "/export", produces = {Const.P_OCTET, Const.P_JSON})
    public void export(String name, @RequestParam(required = false) String fieldNames) {
        ExcelHelper.exportWeb(seqService.selectList(name), SysSeq.class, "系统序列列表",fieldNames);
    }

    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "获取序列导出字段")
    @PostMapping(value = "/exportNames")
    public RestMessage<List<String>> exportNames() {
        return RestMessage.success(ExcelHelper.getExcelFiledNames(SysSeq.class));
    }
}